home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3040 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.9 KB  |  203 lines

  1. Path: nntp.ucsb.edu!usenet
  2. From: "Peter V. Kharchenko" <peter@cs.ucsb.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Basic header file broblem in BC 4.5
  5. Date: 21 Jan 1996 20:48:00 GMT
  6. Organization: UCSB
  7. Message-ID: <4du8q0$k1h@yuggoth.ucsb.edu>
  8. NNTP-Posting-Host: engrhub.ucsb.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (X11; I; OSF1 V2.0 alpha)
  13. X-URL: news:comp.lang.c++
  14.  
  15. Hi everyone,
  16.  
  17.   I've been trying for a several days to correct my project file in some way to
  18. make it work. Here's the situation that I have:
  19.  
  20.  I have two header files (.hpp) with little (.cpp) files corresponding to them,
  21. and one file that includes main() function in it. I am using Borland C 4.5. I
  22. created a project and included there two .cpp files corresponding to headers
  23. and the main file. The problem is that Linker does not recognize the functions
  24. described in one of the .cpp (.hpp) files.  The whole thing works perfectly if
  25. I just attach function bodies (ones that are normally in little .cpp file) to
  26. the .hpp file.  Anyway ..  I'll enclose the files later on.
  27.  
  28.  Actual problem:  I get  a linker error: undifined object  in task01.cpp :
  29. stack<complex>::<complex> pop()
  30. etc. on all the other stack functions. (notice that  all the functions
  31. described in complex.cpp are workign just fine, it's just the stack unit that
  32. is screwed)
  33.  
  34. here's how my project window looks like:
  35. o task01.exe
  36.     o complex.cpp
  37.     o stack.cpp
  38.     o task01.cpp
  39.  
  40. Sources for the files:
  41.  
  42. // task01.cpp
  43. #include <iostream.h>
  44. #include "complex.h"
  45. #include "stack.h"
  46.  
  47. istream& operator>>(istream& s, complex& a)
  48. {
  49.  double re=0, im=0;
  50.  char c=0;
  51.  
  52.  s >> c;
  53.  if (c=='(') {
  54.         s >> re >> c;
  55.         if (c== ',') s >> im >> c;
  56.         if (c!= ')') s.clear(ios::failbit);
  57.  }
  58.  else {
  59.         s.putback(c);
  60.  
  61.  }
  62.  
  63.  if (s) a=complex(re,im);
  64.  return s;
  65. }
  66.  
  67. main() {
  68.  
  69. complex temp;
  70. char c;
  71. Stack<complex> values(40);
  72. do
  73. {
  74.         cin.get(c);
  75.         if (c=='(') {
  76.                 cin.putback(c); cin >> temp;
  77.                 if (cin.fail()) cerr << "Corrupted data !!!\n"; else
  78. values.push
  79. (temp);
  80.                 }
  81.         else if (c=='*') { values.push(values.pop()*values.pop()); }
  82.         else if (c=='+') { values.push(values.pop()+values.pop()); }
  83.         else if (c!='\n') cerr << "Corrupted data !!!\n";
  84. }
  85. while (c!='\n');
  86.  
  87. if (!cin.fail()) cout << "Answer:" << values.pop() << endl;
  88. return(0);
  89. }
  90.  
  91.  
  92. --------------------------------------------------------------------------------
  93.  
  94. //stack.hpp
  95. #ifndef STACK_H
  96. #define STACK_H 1
  97. template <class KeyType> class Stack {
  98.  private:
  99.         int top;
  100.         KeyType *stack;
  101.         int MaxSize;
  102.  
  103.  public:
  104.         Stack(int MaxStackSize = 100);
  105.         int IsFull();
  106.         int IsEmpty();
  107.         void push(const KeyType& item);
  108.         KeyType pop();
  109. };
  110.  
  111.  
  112.  
  113. --------------------------------------------------------------------------------
  114.  
  115. //stack.cpp
  116. template <class KeyType>  Stack<KeyType>::Stack(int
  117. MaxStackSize):MaxSize(MaxStackSize)
  118. {
  119.         stack=new KeyType[MaxSize];
  120.         top=-1;
  121. }
  122.  
  123. template <class KeyType> inline int Stack<KeyType>::IsFull()
  124. {
  125.         if (top == MaxSize-1) return TRUE;
  126.         else return FALSE;
  127. }
  128.  
  129. template <class KeyType> inline int Stack<KeyType>::IsEmpty()
  130. {
  131.         if(top==-1) return TRUE;
  132.         else return FALSE;
  133. }
  134.  
  135. template <class KeyType> void Stack<KeyType>::push(const KeyType& x)
  136. {
  137.         if(IsFull()) cerr << "Error: Stack is FULL !!!";
  138.         else stack[++top]=x;
  139. }
  140.  
  141. template <class KeyType> KeyType Stack<KeyType>::pop()
  142. {
  143.         if(IsEmpty()) {cerr << "Error: Stack is EMPTY !!!"; return 0;}
  144.         return stack[top--];
  145. }
  146. #endif
  147.  
  148.  
  149.  
  150. -------------------------------------------------------------------------------
  151.  
  152. // complex.hpp
  153. #ifndef COMPLEX_H
  154. #define COMPLEX_H 1
  155. #include <iostream.h>
  156. class complex {
  157.         double re, im;
  158. public:
  159.         complex(double,double);
  160.         complex();
  161.  
  162.         friend complex operator+(const complex&,const complex&);
  163.         friend complex operator-(const complex&,const complex&);
  164.         friend complex operator*(const complex&,const complex&);
  165.         friend ostream& operator<<(ostream&,const complex)  ;
  166.         void operator=(const complex& );
  167. };
  168.  
  169.  
  170. --------------------------------------------------------------------------------
  171.  
  172. //complex.cpp
  173. complex::complex(double r, double i =0 ) { re=r; im=i; };
  174. complex::complex() { re=0; im=0; };
  175.  
  176. complex operator+(const complex& lhs,const complex& rhs)
  177.         { return complex(lhs.re+rhs.re,lhs.im+rhs.im);  };
  178.  
  179. complex operator-(const complex& lhs,const complex& rhs)
  180.         { return complex(lhs.re-rhs.re,lhs.im-rhs.im); };
  181.  
  182. complex operator*(const complex& lhs,const complex& rhs)
  183.         { return
  184. complex(lhs.re*rhs.re-lhs.im*rhs.im,lhs.re*rhs.im+lhs.im*rhs.re);};
  185.  
  186. void complex::operator=(const complex& rhs)
  187.         { re=rhs.re; im=rhs.im; }
  188.  
  189. ostream& operator<<(ostream& os, const complex val)
  190. {
  191.          return os << '(' << val.re << ',' << val.im << ')';
  192. }
  193.  
  194. #endif
  195.  
  196.  
  197.  
  198.  
  199. Thanx,
  200.  
  201.  Peter.
  202.  
  203.